package servlet;


import entity.User;
import service.Impl.UserServiceImpl;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;char=utf-8");
        String opr=request.getParameter("opr");
        UserService us=new UserServiceImpl();
        PrintWriter out = response.getWriter();
        if(opr.equals("in")){
            String email=request.getParameter("email");
            String password=request.getParameter("password");
            User user=us.denglu(email,password);
            if(user==null){
                out.print("<script>alert('登录失败');history.go(-1)</script>");
            }else{
                request.getSession().setAttribute("user",user);
                request.getRequestDispatcher("/index.jsp").forward(request,response);
            }
        }else if(opr.equals("up")){
            User user=new User();
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
            String email=request.getParameter("email");
            String password=request.getParameter("password");
            String username=request.getParameter("username");
            System.out.println(username);
            try {
                Date birthday=formatter.parse(request.getParameter("birthday"));
                user.setBirthday(birthday);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            user.setPassword(password);
            user.setUsername(username);
            user.setEmail(email);
            int i=us.init(user);
            if(i==2){
                out.print("<script>alert('用户名已注册');history.go(-1)</script>");
            }else if(i==3){
                out.print("<script>alert('email已注册');history.go(-1)</script>");
            }else if(i==1){
                User user1=us.selectName(username);
                request.getSession().setAttribute("user",user1);
                request.getRequestDispatcher("/index.jsp").forward(request,response);
            }else {
                out.print("<script>alert('注册失败');history.go(-1)</script>");
            }
        }else if(opr.equals("end")){
            request.getSession().removeAttribute("user");
            request.getRequestDispatcher("/index.jsp").forward(request,response);
        }else if(opr.equals("cha")){
            int id=Integer.parseInt(request.getParameter("id"));
            User user=us.selectID(id);
            request.setAttribute("user",user);
            request.getRequestDispatcher("/userUpdate.jsp").forward(request,response);
        }else if(opr.equals("update")){
            User user=new User();
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
            String email=request.getParameter("email");
            int id=Integer.parseInt(request.getParameter("id"));
            String username=request.getParameter("username");
            System.out.println(username);
            try {
                Date birthday=formatter.parse(request.getParameter("birthday"));
                user.setBirthday(birthday);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            user.setUsername(username);
            user.setId(id);
            user.setEmail(email);
            int i=us.update(user);
            if(i==2){
                out.print("<script>alert('用户名已注册！');history.go(-1)</script>");
            }else if(i==3){
                out.print("<script>alert('email已注册！');history.go(-1)</script>");
            }else if(i==1){
                User user1=us.selectName(username);
                request.getSession().setAttribute("user",user1);
                request.getRequestDispatcher("/index.jsp").forward(request,response);
            }else {
                out.print("<script>alert('修改失败！');history.go(-1)</script>");
            }
        }
    }
}
